﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class GroupDAL
    {
        private static Group CreateGroupFromReader(SqlDataReader reader)
        {
            Group group = new Group
            {
                GroupID = Utility.StringParse(reader[GroupColumns.GroupID.ToString()]),
                Order = Utility.IntParse(reader[GroupColumns.Order.ToString()]),
            };

            return group;
        }

        public static List<Group> GetGroupList()
        {
            try
            {
                List<Group> groupList = new List<Group>();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.GroupGetList))
                {
                    while (reader.Read())
                    {
                        groupList.Add(CreateGroupFromReader(reader));
                    }
                }

                return groupList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetGroupList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_GROUP_GETLIST);
            }
        }
    }
}